package com.orange.controller.demo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import lombok.Data;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class BaiduHotDataFetcher {

    // 定义数据结构
    @Data
    public static class HotItem {
        private String id;
        private String title;
        private String url;
        private int hotScore;
        private Extra extra;

        // Getters and Setters
        // 这里省略了getter和setter方法，实际使用时需要添加
    }

    @Data
    public static class Extra {
        private String hover;

        // Getters and Setters
    }

    @Data
    public static class ResponseData {
        private List<Card> data;

        // Getters and Setters
    }

    @Data
    public static class Card {
        private List<Content> content;

        // Getters and Setters
    }

    @Data
    public static class Content {
        private String rawUrl;
        private String word;
        private int hotScore;
        private String desc;
        private boolean isTop;

        // Getters and Setters
    }

    public static void main(String[] args) {
        try {
            List<HotItem> result = fetchBaiduHotData();
            System.out.println("成功获取百度热点数据: " + result);
        } catch (IOException e) {
            System.err.println("获取数据失败: " + e.getMessage());
        }
    }

    public static List<HotItem> fetchBaiduHotData() throws IOException {
        // 1. 获取原始数据
        String rawData = myFetch("https://top.baidu.com/board?tab=realtime");

        // 2. 使用正则表达式提取JSON数据
        Pattern pattern = Pattern.compile("<!--s-data:(.*?)-->", Pattern.DOTALL);
        Matcher matcher = pattern.matcher(rawData);

        if (!matcher.find()) {
            throw new RuntimeException("未找到JSON数据");
        }

        String jsonStr = matcher.group(1);

        // 3. 解析JSON数据
        // 这里使用Gson库来解析JSON
        // 需要先添加Gson依赖: implementation 'com.google.code.gson:gson:2.8.9'
        //ResponseData data = new Gson().fromJson(jsonStr, ResponseData.class);

        JSONArray res = JSONObject.parseObject(jsonStr).getJSONObject("data").getJSONArray("cards").getJSONObject(0).getJSONArray("content");
        for (int i = 0; i < res.size(); i++) {
            System.out.println("内容："+ i + " "+ res.getJSONObject(i).getString("query"));

        }

        // 4. 处理并返回结果
        List<HotItem> result = new ArrayList<>();
        //for (Content content : data.getData().getCards().get(0).getContent()) {
        //    if (!content.isTop()) {
        //        HotItem item = new HotItem();
        //        item.setId(content.getRawUrl());
        //        item.setTitle(content.getWord());
        //        item.setUrl(content.getRawUrl());
        //        item.setHotScore(content.getHotScore());
        //
        //        Extra extra = new Extra();
        //        extra.setHover(content.getDesc());
        //        item.setExtra(extra);
        //
        //        result.add(item);
        //    }
        //}

        return result;
    }

    private static String myFetch(String url) throws IOException {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .build();

        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                throw new IOException("请求失败: " + response);
            }
            return response.body().string();
        }
    }
}